package com.ruoyi.transaction.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.entity.TransactionEntity.ShopOrderItem;
import com.ruoyi.transaction.mapper.ShopOrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.transaction.mapper.ShopOrderMapper;
import com.ruoyi.common.entity.TransactionEntity.ShopOrder;
import com.ruoyi.transaction.service.IShopOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单表Service业务层处理
 * 
 * @author swy
 * @date 2025-08-07
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService 
{
    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopOrderItemMapper shopOrderItemMapper;
    /**
     * 查询订单表
     * 
     * @param id 订单表主键
     * @return 订单表
     */
    @Override
    public ShopOrder selectShopOrderById(Integer id)
    {
        return shopOrderMapper.selectShopOrderById(id);
    }

    /**
     * 查询订单表列表
     * 
     * @param shopOrder 订单表
     * @return 订单表
     */
    @Override
    public List<ShopOrder> selectShopOrderList(ShopOrder shopOrder)
    {
        return shopOrderMapper.selectShopOrderList(shopOrder);
    }

    /**
     * 新增订单表
     *
     * @param shopOrder 订单表
     * @return 结果
     */
//    @Override
//    public int insertShopOrder(ShopOrder shopOrder)
//    {
//        shopOrder.setCreateTime(DateUtils.getNowDate());
//        return shopOrderMapper.insertShopOrder(shopOrder);
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertShopOrder(ShopOrder shopOrder) {
        shopOrder.setCreateTime(DateUtils.getNowDate());
        int rows = shopOrderMapper.insertShopOrder(shopOrder);

        if (rows > 0) {
            Integer orderId = shopOrder.getId();

            if (shopOrder.getItems() != null) {
                shopOrder.getItems().forEach(item -> {
                    item.setOrderId(orderId);
                    item.setCreateTime(DateUtils.getNowDate());
                    shopOrderItemMapper.insertShopOrderItem(item);
                });
            }

            return orderId; // 返回生成的ID
        }

        return 0; // 失败返回0或负数
    }


    /**
     * 修改订单表
     * 
     * @param shopOrder 订单表
     * @return 结果
     */
    @Override
    public int updateShopOrder(ShopOrder shopOrder)
    {
        shopOrder.setUpdateTime(DateUtils.getNowDate());
        return shopOrderMapper.updateShopOrder(shopOrder);
    }

    /**
     * 批量删除订单表
     * 
     * @param ids 需要删除的订单表主键
     * @return 结果
     */
//    @Override
//    public int deleteShopOrderByIds(Integer[] ids)
//    {
//        return shopOrderMapper.deleteShopOrderByIds(ids);
//    }


    @Override
    public int deleteShopOrderByIds(Integer[] ids) {
        return shopOrderMapper.deleteShopOrderByIds(ids);
    }
    /**
     * 删除订单表信息
     * 
     * @param id 订单表主键
     * @return 结果
     */
//    @Override
//    public int deleteShopOrderById(Integer id)
//    {
//        return shopOrderMapper.deleteShopOrderById(id);
//    }
    @Override
    public int deleteShopOrderById(Integer id) {
        return shopOrderMapper.deleteShopOrderById(id);
    }

}
